package com.ruizhong.integratedmediawithoutc.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruizhong.integratedmediawithoutc.dao.TUserMapper;
import com.ruizhong.integratedmediawithoutc.entity.TUser;
import com.ruizhong.integratedmediawithoutc.service.TUserService;
import com.ruizhong.integratedmediawithoutc.util.MD5Hash;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.UUID;

@Service
public class TUserServiceImpl extends ServiceImpl<TUserMapper, TUser> implements TUserService {
    @Override
    public String userLogin(HashMap<String, String> loginForm) {
        if (loginForm.get("user_name") == null  || loginForm.get("password") == null) {
            throw new RuntimeException("请完整填写信息");
        }
        String userName = loginForm.get("user_name").trim();
        String password = loginForm.get("password").trim();
        if (StringUtils.isEmpty(userName) || StringUtils.isEmpty(password)) {
            throw new RuntimeException("请完整填写信息");
        }
        String secretPassword = MD5Hash.md5Hash(password, userName, 3);
        TUser recordUser = lambdaQuery()
                .eq(TUser::getUserName, userName)
                .one();
        if (recordUser == null){
            throw new RuntimeException("该用户不存在");
        }
        if (!secretPassword.equals(recordUser.getPassword())) {
            throw new RuntimeException("密码错误");
        }
        String token = UUID.randomUUID().toString();
        return token;
    }
}
